<?php

// Namespace

namespace Loop;


// Dependencies

require_once(PATH_LOOP . 'Class/IFileFactory.php');


/**
 * @package		Loo
 * @category	Driver
 */

class CCsvFactory implements IFileFactory
{
	/**
	 * Close the file
	 *
	 * @param $object 	$file 	The file
	 */

	public function close(&$file)
	{
		fclose($file);
	}


	/**
	 * Get the columns
	 *
	 * @param $object 	$file 	The file
	 */

	public function getColumns(&$file)
	{
		$columns = fgetcsv($file, 1000, ";");


		return $columns;
	}


	/**
	 * Open the file
	 *
	 * @param 	$object 	$file 		The file
	 * @param 	string 		$option 	The option
	 */

	public function open(&$file, $option)
	{
		if (($handle = fopen($file, $option)) !== false) 
		{
			return $handle;
		}
		else
		{
			e 
			(
				EXCEPTION_FILE_ACCESS_NOT_GRANTED,
				array 
				(
					'file' => $file
				)
			);
		}
	}


	/**
	 * Read
	 *
	 * @param $object 	$file 	The file
	 */

	public function read(&$file)
	{
		$dataRaw = array();

		while (($data = fgetcsv($file, 0, ";",'"','\\')) !== false)
		{
			$dataRaw[] = $data;
		}


		return $dataRaw;
	}

	/**
	 * Write
	 *
	 * @param 	object 		$file 	The file
	 * @param 	array 		$data 	The data
 	 */

	public function write(&$file, $data)
	{
		fputcsv($file, $data, ";",'"');


		return $file;
	}	

}

?>